package com.maimao.model.goods;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author MaoLin Wang
 * @date 2020/11/27 11:19 下午
 */
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "mm_comment")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Comment implements Serializable {


    private static final long serialVersionUID = -998636586405792444L;

    @Id
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;


    @NotBlank(message = "请填写评论内容")
    private String content;

    @Field("user_id")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long userId;

    /**
     * 回复的时候传被回复评论的店铺id，用来判断回复人是否为该评论店铺的商家
     */
    @NotNull(message = "店铺信息非法")
    @Field("shop_id")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long shopId;

    @Field("username")
    private String username;

    @Field("order_id")
    @NotNull(message = "非法信息")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long orderId;

    /**
     * 因为是由前端默认填写的 没有就说明不是正常操作
     */
    @Field("sku_id")
    @NotNull(message = "非法信息")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long skuId;

    @Field("goods_id")
    @NotNull(message = "非法信息")
    @Indexed
    @JsonSerialize(using = ToStringSerializer.class)
    private Long goodsId;

    @NotBlank(message = "非法信息")
    private String spec;

    @JsonSerialize(using = ToStringSerializer.class)
    @NotNull(message = "非法信息")
    private Long pid;

    /**
     * 是否为系统默认评论
     */
    @Field("is_default")
    private Integer isDefault;

    /**
     * 评论中的图片列表 json列表格式
     */
    @Field("picture_list")
    @Size(min = 0, max = 5, message = "最大允许上传5张照片")
    private List<String> pictureList;

    /**
     * 是否含有图片
     */
    @Field("has_picture")
    @NotNull(message = "非法信息")
    private Integer hasPicture;

    /**
     * 顶级id （冗余 可以直接根据顶级id查询其所有子评论，前端不嵌套）
     */
    @Field("top_id")
    @NotNull(message = "非法信息")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long topId;

    /**
     * 0：普通回复评论 1 好评 2 中评 3差评
     */
    @NotNull(message = "请选择评分")
    private Integer type;

    @NotNull(message = "请选择评分")
    private Integer star;

    /**
     * 是否匿名
     */
    @NotNull(message = "请选择是否匿名")
    private Integer anonymous;


    /**
     * 1 : 普通用户 0 商家 2 其他用户
     */
    @Field("from_type")
    @NotNull(message = "非法信息")
    private Integer fromType;

    /**
     * 是否需要解释 商家可以对用户的中差评进行狡辩 0 不需要解释 1 需要解释 2 已经解释(处理)过了
     */
    @Field("need_explain")
    private Integer needExplain;

    @Field("create_time")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss", iso = DateTimeFormat.ISO.DATE_TIME)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;

    /**
     * 是否回复过了
     */
    @Field("replied")
    private Integer replied;

    @Transient
    private List<Comment> children;

    @Override
    public String toString() {
        return "Comment{" +
                "id=" + id +
                ", content='" + content + '\'' +
                ", userId=" + userId +
                ", orderId=" + orderId +
                ", skuId=" + skuId +
                ", shopId=" + shopId +
                ", goodsId=" + goodsId +
                ", spec='" + spec + '\'' +
                ", pid=" + pid +
                ", isDefault=" + isDefault +
                ", pictureList=" + pictureList +
                ", hasPicture=" + hasPicture +
                ", topId=" + topId +
                ", type=" + type +
                ", star=" + star +
                ", anonymous=" + anonymous +
                ", fromType=" + fromType +
                ", needExplain=" + needExplain +
                ", replied=" + replied +
                '}';
    }
}
